package com.ezerg.boot.controller;

import javax.annotation.Resource;

import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.ezerg.boot.bean.TestBean;
import com.ezerg.boot.bean.User;
import com.ezerg.boot.dao.TestMapper;
import com.ezerg.boot.dao.TestMongodb;
import com.ezerg.boot.service.TestService;
import com.ezerg.boot.util.MqUtil;
import com.ezerg.boot.util.RedisUtil;

@Controller
public class TestController {
	
	@Resource
	TestMongodb testMongodb;
	
	@Resource
	TestMapper testMapper;
	
	@Resource
	TestService testService;
	
	@Resource
	private RedisUtil redisUtil;
	
	@Resource
	private MqUtil mqUtil;

	@RequestMapping("hello")
	@ResponseBody
	public String hello() {
		return "hello" ;
	}
	
	@RequestMapping("testMongodb")
	@ResponseBody
	public String testMongodb() {
		User user = new User();
		user.setId(1);
		user.setAge(20);
		user.setName("ezerg");
		testMongodb.insert(user);
		User u = testMongodb.getUser(1);
		return "test = "+u.getName();
	}	
	
	@RequestMapping("testDao")
	@ResponseBody
	public String testDao() {
		TestBean test = testMapper.getTest(1);
		return "test = "+test.getName();
	}
	
	@RequestMapping("testService")
	@ResponseBody
	public String testService() {		
		return testService.test();
	}

	@RequestMapping("freemarker")
    public ModelAndView freemarker(){
        ModelAndView mv=new ModelAndView("freemarker");//模板文件的名称，不需要指定后缀
        mv.addObject("key","hello freemaker");
        return mv;
    }

	@RequestMapping("testRedis")
	@ResponseBody
	public String testRedis() {
		redisUtil.set("key", "hello redis");
		return "redis = "+ redisUtil.get("key");
	}
	
	@RequestMapping("mqSend")
	@ResponseBody
	public String testSend() {
		mqUtil.sendQueueMessage("testq", "我是队列模式的消息");
		mqUtil.sendTopicMessage("testt", "我是主题模式的消息");
		return "sucess";
	}
	
	@JmsListener(destination="testq")
	public void testMq1(Object mess) {
		System.out.println("mqq="+mess);
	}
	
	@JmsListener(destination="testt")
	public void testMq2(Object mess) {
		System.out.println("mqt="+mess);
	}
}
